package com.xmin.lecture.chat;

import com.xmin.lecture.service.Assistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * low-level:更底层的实现，代码相对复杂
 * high-level:代码更简洁，但是自由度相对较低
 */

@RequestMapping("/api/rag")
@RequiredArgsConstructor
@RestController
public class RagAPI {

    final ChatLanguageModel chatLanguageModel;

    @GetMapping("/low/chat")
    public String lowChat(@RequestParam(value = "message") String message) {

        return chatLanguageModel.chat(List.of(SystemMessage.systemMessage("假如你是特朗普，接下来请以特朗普的语气来对话"),
            UserMessage.userMessage(message)
        )).aiMessage().text();

//        return chatLanguageModel.chat(UserMessage.from(message)).aiMessage().text();

    }
    final Assistant assistant;

    @GetMapping("/high/chat")
    public String highChat(@RequestParam(value = "message") String message) {

        return assistant.chat(message);

    }

    final EmbeddingStore<TextSegment> embeddingStore;

    @GetMapping("/load")
    public String load(){
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("D:\\java\\program\\lecture-langchain-new\\lecture-langchain-20250525\\documents");
        EmbeddingStoreIngestor.ingest(documents,embeddingStore);
        return "success";
    }

}
